package com.guoxue.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.guoxue.dao.IGiveAwayRecordDao;
import com.guoxue.domain.GiveAwayRecordVo;
@Repository
public class GiveAwayRecordDaoImpl extends BaseDaoImpl<GiveAwayRecordVo> implements IGiveAwayRecordDao{

	@Override
	public void saveGiveAwayRecord(GiveAwayRecordVo giveAwayVo) {
		saveOrUpdate(giveAwayVo);
	}

	@Override
	public List<GiveAwayRecordVo> getGiveAwayRecordList(Integer uid,Integer tapesId,Integer start,Integer total) {
		return find("FROM GiveAwayRecordVo g WHERE g.uid= ? AND g.tapesId = ? AND g.sysflag = 0", new Object[] {uid,tapesId},start,total);
	}

	@Override
	public Long getGiveAwayRecordTotal(int uid) {
		return count("SELECT count(*) FROM GiveAwayRecordVo g WHERE g.uid = ? AND g.sysflag = 0",new Object[] {uid});
	}

	@Override
	public GiveAwayRecordVo getGiveAwayRecord(int uid) {
		return get("FROM GiveAwayRecordVo g WHERE g.uid = ? AND g.sysflag = 0", new Object[] {uid});
	}

	@Override
	public List<GiveAwayRecordVo> getGiveAwayRecordList(Integer giveUid, Integer tapesId) {
		return find("FROM GiveAwayRecordVo g WHERE g.giveUid= ? AND g.tapesId = ? AND g.sysflag = 0 ORDER BY g.popularityValue DESC", new Object[] {giveUid,tapesId});
	}
	
	@Override
	public Long getGiveAwayRecordPopularityValueTotal(int uid) {
		return count("SELECT sum(popularityValue) FROM GiveAwayRecordVo g WHERE g.giveUid = ? AND g.sysflag = 0",new Object[] {uid});
	}
	
	@Override
	public Map<String,String> getGiveAwayRecordGoodsTotal(int uid) {
		return map("SELECT goodsId,sum(total) AS popularityValue FROM GiveAwayRecordVo where giveUid = ? group by goodsId order by popularityValue DESC",new Object[] {uid});
	}
	
}
